package com.gdziarmaga.webshop.book.services;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.gdziarmaga.webshop.book.model.Book;
import com.gdziarmaga.webshop.book.model.BookDAO;
import com.gdziarmaga.webshop.book.model.BookShortInfo;
import com.gdziarmaga.webshop.utils.filtering.BaseFilter;

@Service("bookManager")
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public class BookManagerImpl implements BookManager, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1652691645923950303L;

	@Autowired
	private BookDAO bookDAO;

	// @Autowired
	// private DozerBeanMapper mapper;

	@Override
	public Book getBookById(Long bookId) {
		return bookDAO.getById(bookId);

	}

	@Override
	public Book getBookByIsbn(String isbn) {
		return bookDAO.findProductByISBN(isbn);

	}

	@Override
	public Book getBookByTitle(String title) {
		return bookDAO.findProductByTitle(title);
	}

	@Override
	public Book getBookByIssue_year(int issue_year) {
		return bookDAO.findProductByIssue_year(issue_year);
	}

	@Override
	public Book getBookByPublisher(String publisher) {
		return bookDAO.findProductByPublisher(publisher);
	}

	@Override
	public Book getBookBySection(String section) {
		return bookDAO.findProductBySection(section);
	}

	@Override
	public Book getBookByPrice(float price) {
		return bookDAO.findProductByPrice(price);
	}

	@Override
	public void saveBook(final Book book) {
		// bookDAO.save(book);
		bookDAO.saveOrUpdate(book);
	}

	@Override
	public List<Book> getBooksByFilterObject(BaseFilter filter) {
		return bookDAO.getAll(filter);
	}

	@Override
	public List<Book> getAll() {
		return bookDAO.getAll();
	}

	@Override
	public List<BookShortInfo> getBooksShortInfosByFilterObject(
			BaseFilter filter) {
		Set<BookShortInfo> infos = new HashSet<BookShortInfo>();
		for (Book book : getBooksByFilterObject(filter)) {
			BookShortInfo bookShortInfo = new BookShortInfo();
			bookShortInfo.setBook_id(book.getBook_id());
			bookShortInfo.setIcon(book.getIconURL());
			bookShortInfo.setPrice(book.getPrice());
			bookShortInfo.setShort_descripton(book.getShort_descripton());
			bookShortInfo.setTitle(book.getTitle());
			infos.add(bookShortInfo);
		}
		return new ArrayList<BookShortInfo>(infos);
	}

	@Override
	public void deleteBook(Long bookId) {
		bookDAO.delete(bookId);
	}

	// public DozerBeanMapper getMapper() {
	// return mapper;
	// }

}
